package com.foreknow.service.impl;

import com.foreknow.dao.impl.GuestbookDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by foreknow on 2022/3/21.
 */
public class GuestbookServiceImpl implements GuestbookService {
    @Override
    public boolean addGuestbook(Guestbook guestbook) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        guestbookDao.setConnection(conn);
        boolean isRigth = false;
        try {
             isRigth = guestbookDao.save(guestbook);
             conn.commit();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return isRigth;
    }

    @Override
    public Guestbook QueryGuestbookById(int id) {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        guestbookDao.setConnection(conn);
        return guestbookDao.getQueryById(id);
    }

    @Override
    public List<Object> queryInfo() {
        DBManager dbManager = DBManager.getInstance();
        Connection conn = dbManager.getConnection();
        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
        guestbookDao.setConnection(conn);
        return guestbookDao.getQueryAll();
    }

//    @Override
//    public boolean save(Guestbook guestbook) {
//        DBManager dbManager = DBManager.getInstance();
//        Connection conn = dbManager.getConnection();
//        GuestbookDaoImpl guestbookDao = new GuestbookDaoImpl();
//        guestbookDao.setConnection(conn);
//        boolean isRigth = false;
//        try {
//            isRigth = guestbookDao.save(guestbook);
//            conn.commit();
//        } catch (SQLException e) {
//            try {
//                conn.rollback();
//            } catch (SQLException ex) {
//                ex.printStackTrace();
//            }
//            e.printStackTrace();
//        }
//        return isRigth;
//    }
}
